﻿Imports SFML
Module ConsoleWindow
    Public WithEvents RenderWindow As Graphics.RenderWindow
    Dim Guy As New Guy
    Dim GuyHitbox As New SFML.Graphics.IntRect(0, 0, 0, 0)
    Dim Background As New SFML.Graphics.Sprite
    Dim Missile As New Missile
    Dim MissileShoot As Short = 0

    Public Sub Main()
        Console.WriteLine("Initializing RenderEngine")
        Try
            RunRenderEngine()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            Console.ReadLine()
        End Try
    End Sub
    Sub RunRenderEngine()
        Console.WriteLine("Loading Charakters")
        Guy.AnimateList("gfx\guy\guy_0.png", 1)
        Missile.AnimateList("gfx\missile\bullet_0.png", 0)
        'Guy.SubRect = GuyHitbox
        Background.Image = New SFML.Graphics.Image("gfx\background.png")

        Console.WriteLine("Create Window")
        RenderWindow = New Graphics.RenderWindow(New Window.VideoMode(800, 600), "FML")
        With RenderWindow
            .ShowMouseCursor(True)
        End With
        Do While RenderWindow.IsOpened
            RenderWindow.DispatchEvents()
            RenderWindow.Clear(New Graphics.Color(0, 0, 0))
            RenderWindow.Draw(Background)
            RenderWindow.Draw(Guy)
            If MissileShoot = 1 Then
                Console.WriteLine("Creating Missile")
                Missile.Shoot(Guy.Rotation, Guy.Position)
                MissileShoot = 2
            ElseIf MissileShoot = 2 Then
                RenderWindow.Draw(Missile)
            End If

            RenderWindow.Draw(New Graphics.String2D("FPS: " & (Math.Round(1 / RenderWindow.GetFrameTime).ToString)))
            RenderWindow.Display()
        Loop
    End Sub

    Private Sub RenderWindow_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles RenderWindow.Closed
        Console.WriteLine("Window closed, dont know what to do! Help!")
        RenderWindow.Close()
    End Sub
    Private Sub RenderWindow_KeyPressed(ByVal sender As Object, ByVal e As SFML.Window.KeyEventArgs) Handles RenderWindow.KeyPressed
        Dim ElapsedTime As Single = RenderWindow.GetFrameTime()
        If e.Code = Window.KeyCode.Escape Then RenderWindow.Close()
        Dim _x As Single = Guy.Position.X
        Dim _y As Single = Guy.Position.Y

        If e.Code = Window.KeyCode.W Then
            'Guy.Move(0, -100 * ElapsedTime)
            Guy.Move(0, -10)
            Guy.Rotation = 0
        End If

        If e.Code = Window.KeyCode.S Then
            Guy.Move(0, +10)
            Guy.Rotation = 180
        End If
      
        If e.Code = Window.KeyCode.D Then
            Guy.Move(+10, 0)
            Guy.Rotation = 270
        End If

        If e.Code = Window.KeyCode.A Then
            Guy.Rotation = 90
            Guy.Move(-10, 0)
        End If

        If e.Code = Window.KeyCode.Space Then
            MissileShoot = 1
            Console.WriteLine("SpaceKey pressed")
        End If
    End Sub

End Module


